function fv=dmean1(data,dnumber)   
%**************************************************
%** This is a supporting function for ML2xtreg.m **
%** Created by Min-Hua Huang, Jan 25, 2012       **
%** data: variables matrix                       **
%** dnumber:  number of variables to be centered **
%** output: variable matrix after being centered **
%**************************************************
code=data(:,dnumber); n=size(code,1); 
m=0; temp=-999999; recordn=ones(1,2)*-999;
for i=1:1:n 
    if code(i)>temp
    temp=code(i);
    m=m+1;
    recordn(m,1)=i;
    end
end
k=size(data,2)-2;
dmean=zeros(n,k); recordn(m+1,1)=n+1;
for i=1:1:m
    first=recordn(i);
    last=recordn(i+1)-1;
    for j=first:1:last
        dmean(j,1:k)=mean(data(first:last,1:k),1);
    end    
    recordn(i,2:k+1)=dmean(first,1:k);
end
%y2gmean=mean(recordn(1:m,2));
fv=dmean;
